SOURCEDIR=.
SOURCES := $(shell find $(SOURCEDIR) -name '*.go')
BINARY=maputnik
VERSION := $(shell node -p "require('../package.json').version")
GOPATH := $(if $(GOPATH),$(GOPATH),$(HOME)/go)
GOBIN := $(if $(GOBIN),$(GOBIN),$(HOME)/go/bin)

all: $(BINARY)

$(BINARY): $(GOBIN)/gox $(GOBIN)/go-winres $(SOURCES) version.go rice-box.go winres/winres.json
	$(GOBIN)/go-winres make --product-version=$(VERSION)
	$(GOBIN)/gox -osarch "windows/amd64 linux/amd64 darwin/amd64" -output "bin/{{.OS}}/${BINARY}"

winres/winres.json: winres/winres_template.json
	sed 's/{{.Version}}/$(VERSION)/g' winres/winres_template.json > $@

$(GOBIN)/go-winres:
	go install github.com/tc-hib/go-winres@latest

# Copy the current release into ./editor/maputnik so it can be
# embedded in the binary
editor/pull_release:
	mkdir -p editor
	cp -r ../dist/* editor

$(GOBIN)/gox:
	go install github.com/mitchellh/gox@v1.0.1

$(GOBIN)/rice:
	go install github.com/GeertJohan/go.rice/rice@v1.0.3

# Embed the current version number in the executable by writing version.go
.PHONY: version.go
version.go:
	@printf "// DO NOT EDIT: Autogenerated by Makefile\n" > version.go
	@printf "package main\n" >> version.go
	@printf "const Version = \"$(VERSION)\"\n" >> version.go

rice-box.go: $(GOBIN)/rice editor/pull_release
	$(GOBIN)/rice embed-go

.PHONY: clean
clean:
	rm -rf editor && rm -f rice-box.go && rm -rf bin
